/*
 * Modified by edhong to include color images, remove unwanted options. 
 * Jan 2008
 *
 * QccPack: Quantization, compression, and coding utilities
 * Copyright (C) 1997-2007  James E. Fowler
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#include "imgdwt.h"

#define USG_STRING "[-w %s:wavelet] [-b %s:boundary] [-nl %d:levels] %s:imgfile %s:subband_pyramid"

QccWAVWavelet Wavelet;
QccString WaveletFilename = QCCWAVWAVELET_DEFAULT_WAVELET;
QccString Boundary = "symmetric";

QccWAVPerceptualWeights PerceptualWeights;
int UsePerceptualWeights = 0;

int NumLevels = 1;
int NumSubbands;

QccIMGImage InputImage;
QccWAVSubbandPyramid SubbandPyramid;

int ImageNumCols;
int ImageNumRows;


int main(int argc, char *argv[])
{
  
  QccInit(argc, argv);

  QccWAVWaveletInitialize(&Wavelet);
  QccIMGImageInitialize(&InputImage);
  QccWAVSubbandPyramidInitialize(&SubbandPyramid);

  if (QccParseParameters(argc, argv,
                         USG_STRING,
                         WaveletFilename,
                         Boundary,
                         &NumLevels,
                         InputImage.filename,
                         SubbandPyramid.filename))
    QccErrorExit();
      
  if (NumLevels < 1)
    {
      QccErrorAddMessage("%s: Number of levels must be 1 or greater",
                         argv[0]);
      QccErrorExit();
    }
  NumSubbands = QccWAVSubbandPyramidNumLevelsToNumSubbands(NumLevels);

  if (QccWAVWaveletCreate(&Wavelet, WaveletFilename, Boundary))
    {
      QccErrorAddMessage("%s: Error calling QccWAVWaveletCreate()",
                         argv[0]);
      QccErrorExit();
    }
  

  if (QccIMGImageRead(&InputImage))
    {
      QccErrorAddMessage("%s: Error calling QccIMGImageRead()",
                         argv[0]);
      QccErrorExit();
    }
  if (QccIMGImageColor(&InputImage))
    {
      printf("%s: Warning! Color images currently are converted to grayscale\n", 
                         argv[0]);
      printf("  When running imgidwt, output to a .pgm file or it will not work\n");
    }

  ImageNumCols = InputImage.Y.num_cols;
  ImageNumRows = InputImage.Y.num_rows;

  SubbandPyramid.num_levels = 0;
  SubbandPyramid.num_rows = ImageNumRows;
  SubbandPyramid.num_cols = ImageNumCols;
  SubbandPyramid.matrix = InputImage.Y.image;
  

  if (QccWAVSubbandPyramidDWT(&SubbandPyramid,
			      NumLevels,
			      &Wavelet))
    {
      QccErrorAddMessage("%s: Error calling QccWAVSubbandPyramidDWT()",
			 argv[0]);
      QccErrorExit();
    }
  
  if (QccWAVSubbandPyramidWrite(&SubbandPyramid))
    {
      QccErrorAddMessage("%s: Error calling QccWAVSubbandPyramidWrite()",
                         argv[0]);
      QccErrorExit();
    }

  QccWAVWaveletFree(&Wavelet);
  QccIMGImageFree(&InputImage);

  QccExit;
}
